<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="__STATIC__/js/jquery.js"></script>
    <script src="__STATIC__/layui/layui.js"></script>
    <script src="__STATIC__/bootstrap-table/js/bootstrap-table.js"></script>
    <!--<script src="__STATIC__/bootstrap-table/js/bootstrap-editable.js"></script>-->
    <script src="__STATIC__/bootstrap-table/js/bootstrap-table-edit.js"></script>
    <script src="__STATIC__/bootstrap-table/js/bootstrap-table-zh-CN.min.js"></script>

    <script src="__STATIC__/bootstrap-table/js/dropdown.js"></script>
    <link rel="stylesheet" href="__STATIC__/bootstrap-table/css/bootstrap-table.min.css">
    <link rel="stylesheet" href="__STATIC__/bootstrap-table/css/bootstrap.css">
    <link rel="stylesheet" href="__STATIC__/bootstrap-table/css/bootstrap-editable.css">
    <script type="text/javascript" src="__STATIC__/x-admin/js/xadmin.js"></script>
    <link rel="stylesheet" href="__STATIC__/x-admin/css/font.css">
    <link rel="stylesheet" href="__STATIC__/x-admin/css/xadmin.css">
</head>
<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="#">用户管理</a>
        <a href="#">用户组管理</a>
        <a href="#">学校管理</a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="layui-row">
    <form class="layui-form layui-col-md12 x-so" onsubmit="return false">
        <!--<input class="layui-input" placeholder="开始日" name="start" id="start">-->
        <!--<input class="layui-input" placeholder="截止日" name="end" id="end">-->
        <input type="text" name="username"  placeholder="学校名称或联系人" autocomplete="off" class="layui-input">
        <button class="layui-btn"  id="search"><i class="layui-icon">&#xe615;</i></button>
    </form>
</div>
<xblock>
    <button class="layui-btn" id="schoolAdd"><i class="layui-icon"></i>添加学校</button>
</xblock>
<table id="schoolList">

</table>
<script>
    layui.use(['laydate','layer'], function(){
        var laydate = layui.laydate;
        layer = layui.layer;
        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
        });
        //执行一个laydate实例
        laydate.render({
            elem: '#end' //指定元素
        });
    });
</script>
<script>
    $('#schoolList').bootstrapTable({
        //获取行号
        language: 'zh-CN',
        todayHighlight: true,
        striped: true,//设置为 true 会有隔行变色效果
        method: 'post',//服务器数据的请求方式 'get' or 'post'
        url: "{:url('/school/schoolList')}",//服务器数据的加载地址
        toolbar:'#toolbar',//
        searchOnEnterKey:true,
        sortName:'school_id',//排序字段
        sortOrder : 'desc',//降序
        sidePagination: "server",   //分页方式：client客户端分页，server服务端分页（*）
        pagination: 'false',//设置为 true 会在表格底部显示分页条
        clickToSelect:'true',//设置true 将在点击行时，自动选择rediobox 和 checkbox
        singleSelect:'true',//设置True 将禁止多选
        pageSize: '15',//如果设置了分页，页面数据条数
        pageList:[15,30,50],
        smartDisplay:false,
        queryParams : function (params) {
            //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            var temp = {
                rows: params.limit,                         //页面大小
                page: (params.offset / params.limit) + 1,   //页码
                sort: params.sort,      //排序列名
                sortOrder: params.order,//排位命令（desc，asc）
                searchName:$("input[name=username]").val()
            };
            return temp;
        },
        columns: [
            //                {title:'请选择',align:'center',checkbox : true},
            {field: 'school_id', title: 'ID', align:'center',sortable : true},
            {field: 'login', title: '登录账号', align:'center'},
            {field: 'school_name', title: '学校名称', align:'center'},
            {field: 'phone_name', title: '联系人', align:'center'},
            {field: 'phone', title: '联系电话', align:'center'},
            {field: 'email', title: '学校邮箱', align:'center'},
            {field: 'url',title: '学校网址', align:'center'},
            {field: 'end',title: '到期时间', align:'center'},
            {field: 'province',title: '省', align:'center',formatter:province},
            {field: 'city',title: '市', align:'center',formatter:city},
            {field: 'district',title: '县/区', align:'center',formatter:county},
            {field: 'address',title: '详细地址', align:'center'},
            {title: '状态', align:'center',formatter:status},
            {align:'center', title:'操作', formatter:operateFormatter}
        ]}
    );
    //操作按钮
    function operateFormatter(val,rows) {
        return [
            "<button type='button' onclick='teacher("+rows.school_id+")' class='btn btn-default btn-xs'>查看教师</button>&nbsp;&nbsp;",
            "<button type='button' onclick='edit("+rows.school_id+")' class='btn btn-primary btn-xs'>修改</button>&nbsp;&nbsp;",
            "<button type='button' onclick='res("+rows.school_id+")' class='btn btn-info btn-xs'>密码重置</button>&nbsp;&nbsp;",
            "<button type='button' onclick='del("+rows.school_id+")' class='btn btn-danger btn-xs'>删除</button>"
        ].join('');
    }
    //省
    function province(val,rows){
        if(rows.province){
            return rows.province.region_name;
        }
    }
    //市
    function city(val,rows) {
        if(rows.city){
            return rows.city.region_name;
        }
    }
    //县
    function county(val,rows){
        if(rows.county){
            return rows.county.region_name
        }
    }
    //状态
    function status(val,rows){
        var date1 = Date.parse(rows.end);
        var date2 = Date.parse(getNowFormatDate());
        dateSpan = date1  - date2;
        if(date1 - date2 > 0){
            if(Math.floor(dateSpan / (24 * 3600 * 1000)) > 30){
                return "<span style='color: blue'>正常</span>";
            }else{
                return "<span style='color: red'>即将到期</span>";
            }
        }else{
            return "<span>以到期</span>";
        }
    }
    //检索操作
    $("#search").click(function(){
        $("#schoolList").bootstrapTable(('refresh'));
    });
    //添加操作
    $("#schoolAdd").click(function(){
        window.location.href = "{:url('/school/add')}"
    });
    //编辑
    function edit(id){
        window.location.href = "{:url('/school/edit')}?id="+id;
    }
    //查看教师
    function teacher(id){
        window.location.href = "{:url('/teacher/index')}?school_id="+id;
    }
    //删除
    function del(id){
        layer.confirm('你确定要删除吗',{btn:['确定','取消']},function(){
            $.ajax({
                url:"{:url('/school/schoolDelete')}",
                type:"post",
                data:{
                    id:id
                },
                dataType:"json",
                success:function(res){
                    if(res.code){
                        layer.msg(res.msg,{icon:1,time:2000},function(){
                            $("#schoolList").bootstrapTable(('refresh'));
                        });
                        return;
                    }else{
                        layer.msg(res.msg,{icon:5,time:2000});
                        return;
                    }
                }
            });
        })
    }
    //密码重置操作
    function res(id){
        layer.confirm('你确定要重置密码吗',{btn:['确定','取消']},function(){
            $.ajax({
                url:"{:url('/school/resetPass')}",
                data:{
                    school_id:id
                },
                type:"post",
                dataType:"json",
                success:function(res){
                    if(res.code){
                        layer.msg(res.msg,{icon:1,time:2000});
                        return;
                    }else{
                        layer.msg(res.msg,{icon:5,time:2000});
                        return;
                    }
                }
            });
        });
    }
    //获取当前时间，格式YYYY-MM-DD
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }
</script>
</body>
</html>